﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WaterBill.BUS;
using WaterBill.DTO;

namespace WaterBill.GUI
{
    public partial class frmManageLiabilities : DevExpress.XtraEditors.XtraForm
    {
        BUS_Bills busBill = new BUS_Bills();
        BUS_Charges busCharge = new BUS_Charges();
        public frmManageLiabilities()
        {
            InitializeComponent();
        }

        private void frmManageLiabilities_Load(object sender, EventArgs e)
        {
            loadDataForm();
        }

        private void loadDataForm()
        {
            try
            {
                DataTable dt = busCharge.BUS_Charges_GetAll();
                if (dt.Rows.Count > 0)
                {
                    lkCharge.Properties.DataSource = dt;
                    lkCharge.Properties.DisplayMember = "NameCharge";
                    lkCharge.Properties.ValueMember = "ChargeId";
                    lkCharge.ItemIndex = 0;

                    lblNameCharge.Text = lkCharge.Text;
                    int ChargeId = Convert.ToInt32(lkCharge.EditValue);
                    gcBill.DataSource = busBill.BUS_Bills_GetByChargeId(ChargeId);
                }
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lkCharge_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                lblNameCharge.Text = lkCharge.SelectedText;
                int ChargeId = Convert.ToInt32(lkCharge.EditValue);
                gcBill.DataSource = busBill.BUS_Bills_GetByChargeId(ChargeId);
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            loadDataForm();
        }

        private void checkStatus_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                int Status = Convert.ToInt32(((CheckEdit)sender).EditValue.ToString());
                int BillId = Convert.ToInt32(gvBill.GetRowCellValue(gvBill.FocusedRowHandle, "BillId").ToString());
                DTO_Bills obj = new DTO_Bills();
                if (Status == 1)
                {
                    obj.BillId = BillId;
                    obj.Status = 2;
                    if (!busBill.BUS_Bills_UpdateStatus(obj))
                    {
                        XtraMessageBox.Show("Lỗi, không thể đánh dấu được", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    if (XtraMessageBox.Show("Hóa đơn này đã được đánh dấu \"đã thu tiền\". Bạn muốn thay đổi nó thành nợ?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        obj.BillId = BillId;
                        obj.Status = 1;
                        if (!busBill.BUS_Bills_UpdateStatus(obj))
                        {
                            XtraMessageBox.Show("Lỗi, không thể đánh dấu được", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
            catch
            {
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsBill = new DataSet();
                DataTable dtBill = new DataTable();
                dtBill.Columns.Add("FullName", typeof(string));
                dtBill.Columns.Add("Address", typeof(string));
                dtBill.Columns.Add("Phone", typeof(string));
                dtBill.Columns.Add("RegisteredDate", typeof(DateTime));
                dtBill.Columns.Add("BillNumber", typeof(string));
                dtBill.Columns.Add("NameCharge", typeof(string));
                dtBill.Columns.Add("OldIndex", typeof(int));
                dtBill.Columns.Add("NewIndex", typeof(int));
                dtBill.Columns.Add("UsedIndex", typeof(int));
                dtBill.Columns.Add("TotalPrice", typeof(decimal));
                dtBill.Columns.Add("Dathu", typeof(string));
                dtBill.Columns.Add("Price", typeof(decimal));
                DataRow dr;
                for (int i = 0; i < gvBill.RowCount; i++)
                {
                    dr = dtBill.NewRow();
                    dr = (DataRow)gvBill.GetDataRow(i);
                    dtBill.ImportRow(dr);
                }
                dsBill.Tables.Add(dtBill);

                rptListBillByCharge rptBill = new rptListBillByCharge();
                rptBill.DataSource = dsBill;
                rptBill.DataMember = dsBill.Tables[0].TableName;
                rptBill.lblFullName.DataBindings.Add("Text", null, "FullName");
                rptBill.lblAddress.DataBindings.Add("Text", null, "Address");
                rptBill.lblBillNumber.DataBindings.Add("Text", null, "BillNumber");
                rptBill.lblNameCharge.DataBindings.Add("Text", null, "NameCharge");
                rptBill.lblPrice.DataBindings.Add("Text", null, "Price", "{0:0,0 vnđ}");
                rptBill.lblOldIndex.DataBindings.Add("Text", null, "OldIndex");
                rptBill.lblNewIndex.DataBindings.Add("Text", null, "NewIndex");
                rptBill.lblUsedIndex.DataBindings.Add("Text", null, "UsedIndex");
                rptBill.lblTotalPrice.DataBindings.Add("Text", null, "TotalPrice", "{0:0,0 vnđ}");
                rptBill.lblStatus.DataBindings.Add("Text", null, "Dathu");
                rptBill.PrintDialog();
            }
            catch
            {
                XtraMessageBox.Show("Lỗi, đã xảy ra lỗi không xác định. Xin vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}